/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.superduperbudget.central.core;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import se.superduperbudget.central.db.SuperDuperUserBank;

/**
 *
 * @author Emil
 */
public class SyncDB {
    public static void syncAll() {
        EntityManager em = EMF.get().createEntityManager();
        
        String query = "select ub from SuperDuperUserBank ub";
        TypedQuery<SuperDuperUserBank> ubs = em.createQuery(query, SuperDuperUserBank.class);
        
        // TODO: Some kind of timing, refreshing accounts over the day
        for (SuperDuperUserBank ub : ubs.getResultList()) {
            Sync.sync(ub);
            
            EntityTransaction tx = em.getTransaction();
            tx.begin();
            em.persist(ub);
            tx.commit();
        }
        
        em.close();        
    }
}
